###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This is a batch file to run replication code serially, by parts
########################################################

rm(list=ls())

do.install=F
# the following packages are called and must be installed
if(do.install==T){
  install.packages('ggplot2',dependencies=TRUE)
  install.packages('stringr',dependencies=TRUE)
  install.packages('car',dependencies=TRUE)
  install.packages('cregg',dependencies=TRUE)
  install.packages('xtable',dependencies=TRUE)
  install.packages('MCMCpack',dependencies=TRUE)
  install.packages('psych',dependencies=TRUE)
  install.packages('grf',dependencies=TRUE)
  install.packages('glmnet',dependencies=TRUE)
  install.packages('foreign',dependencies=TRUE)
  install.packages('matrixStats',dependencies=TRUE)
  install.packages('Rcpp',dependencies=TRUE)
}
# R script to run rreplication code in batch
dirs='~/Desktop/replication/'
setwd(dirs)

# may want to move old run logs to archive folder: this system call works for bash in R
# ::
system(paste('mv log/*.Routs log/old_runs/'))

do.build=T
#data build files; note, data files are pre-compiled, but can be reproduced using the objects in /data_base folder
if(do.build==T){
  cat('##########\nRunning File 1\n##########')
  sink(file='log/1_build_score_libcon_i.Routs')
  source('build/1_build_score_libcon_i.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 2\n##########')
  sink(file='log/2_build_score_libcon_ii.Routs')
  source('build/2_build_score_libcon_ii.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 3\n##########')
  sink(file='log/3_build_score_affinity.Routs')
  source('build/3_build_score_affinity.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 4\n##########')
  sink(file='log/4_build_sophistication_indices.Routs')
  source('build/4_build_sophistication_indices.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 5\n##########')
  sink(file='log/5_build_cross_pressure_covars.Routs')
  source('build/5_build_cross_pressure_covars.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 6\n##########')
  sink(file='log/6_build_libcon_correct_index.Routs')
  source('build/6_build_libcon_correct_index.R',echo=TRUE)
  sink()
}

do.main=T
# main analyses
if(do.main==T){
  cat('##########\nRunning File 7\n##########')
  sink(file='log/7_libcon_global.Routs')
  source('main/7_libcon_global.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 8\n##########')
  sink(file='log/8_choice_twoparty.Routs')
  source('main/8_choice_twoparty.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 9\n##########')
  sink(file='log/9_choice_primary_twoparty_affinity.Routs')
  source('main/9_choice_primary_twoparty_affinity.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 10\n##########')
  sink(file='log/10_choice_H_M_L_stratify.Routs')
  source('main/10_choice_H_M_L_stratify.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 11\n##########')
  sink(file='log/11_consistency_agreement_results.Routs')
  source('main/11_consistency_agreement_results.R',echo=TRUE)
  sink()
}

do.appendix=T
#appendix analyses
if(do.appendix==T){
  cat('##########\nRunning File 12\n##########')
  sink(file='log/12_score_density.Routs')
  source('appendix/12_score_density.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 13\n##########')
  sink(file='log/13_summary_policy_sophistication_analysis.Routs')
  source('appendix/13_summary_policy_sophistication_analysis.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 14\n##########')
  sink(file='log/14_summary_policy_sophistication_analysis_common.Routs')
  source('appendix/14_summary_policy_sophistication_analysis_common.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 15\n##########')
  sink(file='log/15_mixed_up_on_difference_issues.Routs')
  source('appendix/15_mixed_up_on_difference_issues.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 16\n##########')
  sink(file='log/16_libcon_H_M_L_stratify.Routs')
  source('appendix/16_libcon_H_M_L_stratify.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 17\n##########')
  sink(file='log/17_libcon_know_stratify.Routs')
  source('appendix/17_libcon_know_stratify.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 18\n##########')
  sink(file='log/18_choice_know_stratify.Routs')
  source('appendix/18_choice_know_stratify.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 19\n##########')
  sink(file='log/19_choice_correct_place_stratify.Routs')
  source('appendix/19_choice_correct_place_stratify.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 20\n##########')
  sink(file='log/20_libcon_twoparty.Routs')
  source('appendix/20_libcon_twoparty.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 21\n##########')
  sink(file='log/21_choice_twoparty_cregg.Routs')
  source('appendix/21_choice_twoparty_cregg.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 22\n##########')
  sink(file='log/22_choice_primary_twoparty_by_election_engagement.Routs')
  source('appendix/22_choice_primary_twoparty_by_election_engagement.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 23\n##########')
  sink(file='log/23_choice_primary_twoparty_by_self_report_engagement.Routs')
  source('appendix/23_choice_primary_twoparty_by_self_report_engagement.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 24\n##########')
  sink(file='log/24_consistencyFixed.Routs')
  source('appendix/24_consistencyFixed.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 25\n##########')
  sink(file='log/25_choice_primary_twoparty_affinity_agreement_stratifications.Routs')
  source('appendix/25_choice_primary_twoparty_affinity_agreement_stratifications.R',echo=TRUE)
  sink()

  ########################################
  # This file uses the grf package. We have noticed some instability in the code,
  #  esp. forced crases across different machines and R implementations. Our
  #  best guess on a fix is to reinstall/reload the Rcpp package before
  #  calling the grf package used in this code. Also note, these results takes
  #  a long while to finish running.
  ########################################
  do.hetfx=T
  if(do.hetfx==T){
    cat('##########\nRunning File 26\n##########')
    sink(file='log/26_heterogeneous.Routs')
    source('appendix/26_heterogeneous.R',echo=TRUE)
    sink()
  }

  cat('##########\nRunning File 27\n##########')
  sink(file='log/27_descriptives_affinity_scores.Routs')
  source('appendix/27_descriptives_affinity_scores.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 28\n##########')
  sink(file='log/28_mixed_partisans_agreement_consistency_twoparty_imputation_full_policies_inparty.Routs')
  source('appendix/28_mixed_partisans_agreement_consistency_twoparty_imputation_full_policies_inparty.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 29\n##########')
  sink(file='log/29_choice_primary_twoparty_affinity_ee.Routs')
  source('appendix/29_choice_primary_twoparty_affinity_ee.R',echo=TRUE)
  sink()

  cat('##########\nRunning File 30\n##########')
  sink(file='log/30_agreement_consistency_twoparty_notimputed_policies_inparty.Routs')
  source('appendix/30_agreement_consistency_twoparty_notimputed_policies_inparty.R',echo=TRUE)
  sink()
}



#END
